package info.qingshui.browser.hk.proxy;

import java.io.Serializable;

import android.util.Log;

@SuppressWarnings("rawtypes")
public class Proxy implements Comparable , Serializable{
	private static final long serialVersionUID = -6773972953341047709L;
	
	private String id;
	private String host = "";//没有代理
	private int port = 0;
	private String type = "HTTP";//HTTP,SOCK4等
	
	
	private int invokeCount = 0;//开始连续失败后调用次数
	private int failCount = 0;//最后连续失败次数
	
	public Proxy() {
		super();
	}
	
	public Proxy(String host, int port) {
		super();
		this.host = host;
		this.port = port;
	}
	
	public void addInvokeCount() {
		
		if( invokeCount >= failCount ) {
			//说明上次成功
			invokeCount = 0;
			failCount = 0;
		}
		if ( failCount > 0 ) {
			invokeCount = invokeCount + 1;
		}
		Log.d(Proxy.class.getSimpleName(), "addInvokeCount:" + host + "|" + port + "|" + this.invokeCount + "|" + this.failCount);
	}
	
	//通知本次代理处理结果成功
//	public void notifySucess() {
//		failCount = 0;
//		Log.d(Proxy.class.getSimpleName(), "notifySucess:" + host + "|" + port + "|" + this.invokeCount + "|" + this.failCount);
//	}
	
	public void addFailCount(  ) {
		failCount = failCount + 1;

		Log.d(Proxy.class.getSimpleName(), "addFailCount:" + host + "|" + port + "|" + this.invokeCount + "|" + this.failCount);

	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public int getInvokeCount() {
		return invokeCount;
	}

	public int getFaileCount() {
		return failCount;
	}

	public String getKey() {
		return this.host + ":" +  this.port;
	}
	
	
	
//	public double getFailRate() {
//		return this.failCount/this.invokeCount;
//	}

	public int getFailCount() {
		return failCount;
	}

	public void setFailCount(int failCount) {
		this.failCount = failCount;
	}

	@Override
	public int compareTo(Object another) {
		Proxy a = (Proxy)another;
		if ( this.getFaileCount() >= a.getFaileCount() ) {
			return 1;
		}
		return -1;
	}

	@Override
	public boolean equals(Object o) {
		if( o == null ) {
			return false;
		}
		Proxy p = (Proxy)o;
		return this.getKey().equals( p.getKey() );
	}
	
	public String toString() {
		return host + ":" + port + "|" + this.invokeCount + "|" + this.failCount;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setInvokeCount(int invokeCount) {
		this.invokeCount = invokeCount;
	}
	
	
}
